package com.gin.no_gay_alliance.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.enums.SubForumType;
import com.gin.no_gay_alliance.activity.TopicListActivity;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.param.TopicListParam;
import com.gin.no_gay_alliance.utils.ImageUtils;
import com.gin.no_gay_alliance.utils.TextViewTarget;
import com.gin.no_gay_alliance.viewmodel.EntryData;

/**
 * 版面入口组件
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/31 14:27
 **/
public class ForumEntryView extends androidx.appcompat.widget.AppCompatTextView implements View.OnClickListener {
    /**
     * 版面icon放置的位置:左/上/右/下/;
     */
    @IntRange(from = 0, to = 3)
    int position = 0;
    /**
     * 版面icon的大小
     */
    int size = 48;
    /**
     * 版面icon的地址
     */
    String icon;
    /**
     * 类型
     */
    SubForumType type;
    /**
     * id
     */
    Long id;

    public ForumEntryView(@NonNull Context context) {
        super(context);
    }

    public ForumEntryView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ForumEntryView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 初始化
     *
     * @param text     文本
     * @param position icon位置
     * @param size     icon尺寸
     * @param icon     icon地址
     * @param type     入口类型
     * @param id       forumId 或 ColTid
     */
    public void init(String text, @IntRange(from = 0, to = 3) int position, int size, String icon, SubForumType type, Long id) {
        setText(text);
        this.position = position;
        this.size = size;
        this.icon = icon;
        this.type = type;
        this.id = id;

        setOnClickListener(this);
        loadIcon();
    }

    /**
     * 初始化
     *
     * @param entryData 入口数据
     * @param position  icon位置
     * @param size      icon尺寸
     */
    public void init(EntryData entryData, @IntRange(from = 0, to = 3) int position, int size) {
        init(entryData.name, position, size, entryData.icon, entryData.type, entryData.id);
    }

    /**
     * 加载版面icon
     */
    public void loadIcon() {
        int s = (int) (this.size * MyApplication.DEVICE_DENSITY_DPI / 160f);
        if (!ObjectUtils.isEmpty(icon)) {
            ImageUtils.loadForumAvatar(getContext(), icon, new TextViewTarget(this, position, s, s));
        }
    }

    @Override
    public void onClick(View v) {
        // 打开版面或合集
        TopicListActivity.startActivity(getContext(), new TopicListParam(type, id, getText().toString()));
    }
}
